<?php

namespace controller;

class Settings extends Shop {

    public function index() {
        $session = \library\Registry::get("session");
        if (!$session->get("customer") && !$session->get("login")) {
            $session->set("last_url", \library\RequestMethods::server("request_uri"));
            $this->redirect($this->shop->URL . "/users/login");
        }
        $view = $this->getActionView();
        $layoutView = $this->getLayoutView();
        if (\library\RequestMethods::post("ProductsPerPage")){
            $this->Customer->DefaultProductOrder=  \library\RequestMethods::post("Order");
            $this->Customer->DefaultAdress=  \library\RequestMethods::post("adressID");
            $this->Customer->DefaultPayment= \library\RequestMethods::post("paymentID");
            $this->Customer->DefaultDelivery=  \library\RequestMethods::post("deliveryID");
            $this->Customer->ProductsPerPage=  \library\RequestMethods::post("ProductsPerPage");
            $this->Customer->MessagesHitory=  \library\RequestMethods::post("messagesAmount");
            $this->Customer->save();
            $session->erase("customer");
            $session->set("customer",  $this->Customer);
        }
        $layoutView->set("paneltitle", "Panel Użytkownika");
        $view->set("customer", $session->get("customer"));
        $adressCriteria = new \model\Criteria();
        $adressCriteria->from(\model\TblAdress::getModel()->getTable())
                ->join(\model\TblPerson::getModel())
                ->join(\model\TblStreet::getModel())
                ->join(\model\TblCity::getModel())
                ->join(\model\TblPostCode::getModel())
                ->where(\model\TblPersonAdress::PERSON_ID, "=", $this->person->PersonID, \model\TblAdress::getModel()->getTable());
        $adresses = \model\TblPersonAdress::getModel()->all($adressCriteria);
        $modelData = new \model\DataTable($adresses, false);
        $paymentsCriteria = new \model\Criteria();
        $paymentsCriteria->from(\model\TblPayment::getModel()->getTable())
                ->where(\model\TblPayment::SHOP_ID, "=", $this->shop->ShopID);
        $payments = \model\TblPayment::getModel()->all($paymentsCriteria);
        $paymentsDataTable = new \model\DataTable($payments);

        $deliveryCriteria = new \model\Criteria();
        $deliveryCriteria->from(\model\TblDeliveryMethod::getModel()->getTable())
                ->where(\model\TblDeliveryMethod::SHOP_ID, "=", $this->shop->ShopID);
        $deliveryMethods = \model\TblDeliveryMethod::getModel()->all($deliveryCriteria);
        $deliveryCriteriaDataTable = new \model\DataTable($deliveryMethods);
        //var_dump($paymentsDataTable->getModelData(true));
        $view->set("deliveryMethods", $deliveryCriteriaDataTable->getModelData(true));
        $view->set("payments", $paymentsDataTable->getModelData(true));
        $view->set("adresses", $modelData->getModelData(false));
        $this->render();
    }

}
